
package com.leyou.item.controller;

import com.leyou.common.pojo.PageResult;
import com.leyou.item.pojo.*;
import com.leyou.item.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    @GetMapping("spu/page")
    ResponseEntity<PageResult<SpuBo>> querySpuByPage(
            @RequestParam(value = "page",defaultValue = "1")Integer page,
            @RequestParam(value = "rows",defaultValue = "5")Integer rows,
            @RequestParam(value = "saleable",required = false)Boolean saleable,
            @RequestParam(value = "key",required = false)String key
    ){

        PageResult<SpuBo> result = this.goodsService.querySpuByPageAndSort(key, saleable, page, rows);
        if(result == null){
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        System.out.println(result.getItems().size());
        for(SpuBo spu : result.getItems()){
            System.out.println(spu);
        }
        return ResponseEntity.ok(result);
    }

    @PostMapping("goods")
    public ResponseEntity<Void> saveGoods(@RequestBody SpuBo spuBo){
        try {
            this.goodsService.save(spuBo);
            return new ResponseEntity<>(HttpStatus.CREATED);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
        }
    }

    @GetMapping("spu/detail/{id}")
    public ResponseEntity<SpuDetail> querySpuDetailById(@PathVariable("id")Long id){
        SpuDetail detail = this.goodsService.querySpuDetailBySpuId(id);
        if(detail==null){
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        return ResponseEntity.ok(detail);
    }

    @GetMapping("sku/list")
    public ResponseEntity<List<Sku>> querySkuBySpuId(@RequestParam("id")Long spuId){
        List<Sku> skus = this.goodsService.querySkuListBySpuId(spuId);
        if(skus.size() == 0 || skus == null){
            return  new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        return ResponseEntity.ok(skus);
    }

    @PutMapping("goods")
    public ResponseEntity<Void> updateGood(@RequestBody SpuBo spuBo){
        try {
            this.goodsService.updateSpu(spuBo);
            return new ResponseEntity<>(HttpStatus.NO_CONTENT);
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
        }
    }

    @PutMapping("goods/saleable/{id}/{saleable}")
    public ResponseEntity<Void> updateSaleableById(@PathVariable("id")Long id,@PathVariable("saleable")Boolean saleable){

        try {
            this.goodsService.updateSaleableById(id,saleable);
            return new ResponseEntity<>(HttpStatus.NO_CONTENT);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
        }
    }
    /**
     * 根据skuid集合查询sku集合
     * @return
     */
    @GetMapping("skus")
    public ResponseEntity<List<SkuSon>> querySkusBySkuIds(@RequestParam("skuIds")List<Long>ids){
        List<SkuSon> skus = this.goodsService.querySkusBySkuIds(ids);
        for (SkuSon skuSon : skus) {

            System.out.println(skuSon.getSaleable());
        }
        if (CollectionUtils.isEmpty(skus)) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(skus);
    }

    @DeleteMapping("goods/deleteById/{id}")
    public ResponseEntity<Void> deleteGoodById(@PathVariable("id")Long id){

        try {
            this.goodsService.deleteGoodById(id);
            return new ResponseEntity<>(HttpStatus.NO_CONTENT);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
        }
    }

    @GetMapping("spu/{id}")
    public ResponseEntity<Spu> querySpuById(@PathVariable("id")Long id){
        Spu spu = this.goodsService.querySpuById(id);
        if(spu == null){
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        return ResponseEntity.ok(spu);
    }

    @GetMapping("sku/{id}")
    public ResponseEntity<Sku> querySkuById(@PathVariable("id")Long id){
        Sku sku = this.goodsService.querySkuById(id);
        if(sku==null){
            return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
        }
        return ResponseEntity.ok(sku);
    }

    @PostMapping("stock/reduce")
    public ResponseEntity<Integer> reduceStock(@RequestParam("skuId") Long skuId,@RequestParam("num") Long num){
        int i = this.goodsService.reduceStock(skuId, num);
        return ResponseEntity.ok(i);
    }


    @GetMapping("queryseckills")
    public ResponseEntity<List<Seckill>> queryseckills(){
        List<Seckill> seckillList = goodsService.querySeckills();
        if (CollectionUtils.isEmpty(seckillList)){
            return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
        }
        return ResponseEntity.ok(seckillList);
    }

    @GetMapping("queryseckparams/{skuid}")
    public ResponseEntity<Map<String,Object>> queryqueryseckparams(@PathVariable("skuid") Long skuid){
        Map<String,Object> map= goodsService.queryseckparams(skuid);
        if (map==null){
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        return ResponseEntity.ok(map);

    }

    @GetMapping("spuBos/{num}")
    public ResponseEntity<List<SpuBo>> querySpuBosByCidAndNum(@RequestParam("cid")Long cid,@PathVariable("num")Integer num) {
        List<SpuBo> spuBos = goodsService.querySpuBosByCidAndNum(cid,num);
        if(spuBos==null){
            return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
        }
        return ResponseEntity.ok(spuBos);
    }
    /**
     * 后台查询秒杀商品并分页
     * @param page
     * @param rows
     * @return
     */
    @GetMapping("seckills/page")
    ResponseEntity<PageResult<Seckill>> querySpuByPage(
            @RequestParam(value = "page",defaultValue = "1")Integer page,
            @RequestParam(value = "rows",defaultValue = "5")Integer rows
    ){

        PageResult<Seckill> result = this.goodsService.querySpuByPage(page, rows);
        if(result == null){
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        System.out.println(result.getItems().size());
        for(Seckill seckill : result.getItems()){
            System.out.println(seckill);
        }
        return ResponseEntity.ok(result);
    }

}

